package com.action.pc.rebate;

import com.action.pc.member.MemberInfoVo;
import com.bean.Cash_apply;
import com.bean.Mb_account;
import com.bean.Member;
import com.bean.Sys_user;
import com.common.BaseAction;
import com.common.ModelAndJson;
import com.github.pagehelper.Page;
import com.service.cash.CashService;
import com.service.member.MbAccountService;
import com.service.member.MemberService;
import com.service.rebate.RebateService;
import com.util.Config;
import com.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by lvjianqing on 2017/9/16.
 */
@Controller
@RequestMapping("rebate")
public class RebateAction extends BaseAction {
    Logger log = Logger.getLogger(RebateAction.class);
    @Autowired
    private RebateService rebateService;
    @Autowired
    private MbAccountService mbAccountService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private CashService cashService;

    @RequestMapping("list")
    public ModelAndView list(HttpServletRequest request, ModelMap modelMap, RebateVo vo) {
        int pageNum = vo.getPageNumInt();
        int pageSize = vo.getPageSizeInt();
        Sys_user user = getSysUser();
        //佣金提取只针对合作商
        Member member = memberService.getMemberByMerchantId(user.getMerchantid());
        HashMap<String, Object> paraMap = new HashMap<>();
        paraMap.put("memberid", member.getId());
        //操作类型 1： 返利 2：提现申请中 3:提现成功 4：提现失败
        paraMap.put("type", vo.getType());
        paraMap.put("cityid", vo.getCityid());
        //返利订单号
        paraMap.put("orderno", vo.getOrderno());
        //提现申请编号
        paraMap.put("cashapplyid", vo.getCashapplyid());
        //返利等级
        paraMap.put("lv", vo.getLv());
        paraMap.put("starttime", vo.getStarttime());
        paraMap.put("endtime", vo.getEndtime());
        Page page = rebateService.findByPage(pageNum, pageSize, paraMap);
        modelMap.put("page", page);
        modelMap.put("pages", page.getPages());
        modelMap.put("pageNum", page.getPageNum());
        modelMap.put("pageSize", page.getPageSize());
        modelMap.put("vo", vo);
        return new ModelAndView("/pc/rebate_list", modelMap);
    }

    /**
     * 会员账户
     *
     * @param request
     * @param modelMap
     * @param vo
     * @return
     */
    @RequestMapping("account")
    public ModelAndView account(HttpServletRequest request, ModelMap modelMap, MemberInfoVo vo) {
        Sys_user user = getSysUser();
        Mb_account account = mbAccountService.selectByMerchantId(user.getMerchantid());
        if (account == null) {
            modelMap.put("id", user.getMerchantid());
            return new ModelAndView("redirect:/merchant/qrcode.htm", modelMap);
        }
        modelMap.put("account", account);
        Float prop = StringUtil.String2Float(Config.getString("cash_discount", "0.03"));
        modelMap.put("prop", prop*100);
        return new ModelAndView("/pc/member_account_cash", modelMap);
    }

    /**
     * 佣金提现
     * @param request
     * @param model
     * @param vo
     * @return
     */
    @RequestMapping("cash")
    @ResponseBody
    public String cash(HttpServletRequest request, ModelAndJson model, RebateVo vo) {
        Sys_user user = getSysUser();
        if (user != null) {
            Member member = memberService.getMemberByMerchantId(user.getMerchantid());
            Mb_account account = mbAccountService.selectByMerchantId(user.getMerchantid());
            if (account != null && member!=null) {
                BigDecimal amount = vo.getAmount();
                BigDecimal rebate = account.getRebate();
                String realname = vo.getRealname();
                double x = rebate.subtract(amount).doubleValue();
                if (x >= 0) {
                    Byte paytype = vo.getPaytype();
                    Cash_apply cash = new Cash_apply();
                    cash.setAmount(amount);
                    cash.setCreaettime(new Date());
                    cash.setPaytype(paytype);
                    cash.setRealname(realname);
                    cash.setMemberid(member.getId());
                    cash.setCityid(user.getCityid());
                    cash.setCategory((byte) 1);
                    if (paytype != null) {//支付宝
                        if (paytype == 1) {
                            String aliaccount = vo.getAliaccount();
                            if(StringUtil.isNotEmpty(aliaccount)){
                                cash.setAliaccount(aliaccount);
                                try {
                                    cashService.saveCashRebate(cash,account);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    model.setFalse("-1", e.getMessage());
                                }
                            }else{
                                model.setFalse("-1", "支付宝账号不存在");
                            }
                        } else if (paytype == 2) {
                            String bankname = vo.getBankname();
                            String bankaccount = vo.getBankaccount();
                            String bankplace = vo.getBankplace();
                            if(StringUtil.isNotEmpty(bankname) && StringUtil.isNotEmpty(bankaccount) && StringUtil.isNotEmpty(bankplace)){
                                cash.setBankname(bankname);
                                cash.setBankaccount(bankaccount);
                                cash.setBankplace(bankplace);
                                try {
                                    cashService.saveCashRebate(cash,account);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    model.setFalse("-1", e.getMessage());
                                }
                            }else{
                                model.setFalse("-1", "银行信息不全");
                            }
                        }
                    } else {//银行卡
                        model.setFalse("-1", "非法操作");
                    }
                } else {
                    model.setFalse("-1", "提现金额大于账户余额");
                }
            } else {
                model.setFalse("-1", "账户不存在");
            }
        } else {
            model.setFalse("-1", "请登录后操作");
        }
        return model.toString();
    }

}
